package cn.edu.xmu.javaee.productdemoaop.dao;
import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.productdemoaop.dao.OnSaleDao;
import cn.edu.xmu.javaee.productdemoaop.dao.bo.OnSale;
import cn.edu.xmu.javaee.productdemoaop.dao.bo.Product;
import java.time.LocalDateTime;
import cn.edu.xmu.javaee.productdemoaop.repository.OnSaleRepository;
import cn.edu.xmu.javaee.productdemoaop.repository.ProductRepository;
import cn.edu.xmu.javaee.productdemoaop.repository.entity.Productjpa;
import cn.edu.xmu.javaee.productdemoaop.repository.entity.OnSalejpa;

import cn.edu.xmu.javaee.productdemoaop.util.CloneFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Ming Qiu
 **/
@Repository
public class JpaDao {
    private final static Logger logger = LoggerFactory.getLogger(JpaDao.class);

    private ProductRepository productRepository;

    private OnSaleRepository onSaleRepository;

    @Autowired
    public JpaDao(ProductRepository productRepository, OnSaleRepository onSaleRepository) {
        this.productRepository = productRepository;
        this.onSaleRepository = onSaleRepository;
    }

    public List<Product> retrieveProductByName(String name, boolean all) throws BusinessException {
        List<Product> productList = new ArrayList<>();
        List<Productjpa> productjpaList = productRepository.findAllByName(name);
        for (Productjpa productjpa : productjpaList){
            Product product = null;
            if (all) {
                product = this.retrieveFullProduct(productjpa);
            } else {
                product = CloneFactory.copy(new Product(), productjpa);
            }
            productList.add(product);
        }
        logger.debug("retrieveProductByName: productList = {}", productList);
        return productList;
    }

    private Product retrieveFullProduct(Productjpa productjpa) throws DataAccessException{
        assert productjpa != null;
        Product product =  CloneFactory.copy(new Product(), productjpa);
        LocalDateTime now = LocalDateTime.now();
        List<OnSalejpa> latestOnSalelist = onSaleRepository.findAllByProductIdAndBeginTimeLessThanEqualAndEndTimeGreaterThan(productjpa.getId(),now,now);
        List<OnSale> latestOnSale = new ArrayList<>();
        for (OnSalejpa onSalejpa : latestOnSalelist){
            OnSale onSale = CloneFactory.copy(new OnSale(), onSalejpa);
            latestOnSale.add(onSale);
        }
        product.setOnSaleList(latestOnSale);

        List<Product> otherProduct = this.retrieveOtherProduct(productjpa);
        product.setOtherProduct(otherProduct);

        return product;
    }

    private List<Product> retrieveOtherProduct(Productjpa productjpa) throws DataAccessException {
        assert productjpa != null;

        // 获取相关产品列表
        List<Productjpa> otherProducts = productRepository.findAllByGoodsIdAndIdNot(productjpa.getGoodsId(), productjpa.getId());
        //System.out.println("retrieveOtherProduct: relatedProducts.size() = " + (relatedProducts != null ? relatedProducts.size() : 0));

        List<Product> productList = new ArrayList<>();
        if (otherProducts != null) {
            for (Productjpa otherProduct : otherProducts) {
                // 跳过当前产品自身
                if (!otherProduct.getId().equals(productjpa.getId())) {
                    Product product = CloneFactory.copy(new Product(), otherProduct);
                    productList.add(product);
                }
            }
        }

        return productList;
    }

}
